﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Common;
using System.Data.SqlTypes;
using System.Data;

namespace TeamTasker.Server.DataAccess.Support
{
    public class DaoUtils
    {
        public static void SetDateTime(IDbParameters param, string name, DateTime? value)
        {
            if (value == null)
            {
                param.Add(name, SqlDbType.DateTime).Value = SqlDateTime.Null;
            }
            else
            {
                param.Add(name, SqlDbType.DateTime).Value = value;
            }
        }

        public static void SetVarchar(IDbParameters param, string name, string value)
        {
            if (value == null)
            {
                param.Add(name, SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                param.Add(name, SqlDbType.NVarChar).Value = value;
            }
        }

        public static void SetText(IDbParameters param, string name, string value)
        {
            if (value == null)
            {
                param.Add(name, SqlDbType.Text).Value = DBNull.Value;
            }
            else
            {
                param.Add(name, SqlDbType.Text).Value = value;
            }
        }

        public static DateTime? GetDateTime(IDataReader reader, int paramIdx)
        {
            if (reader.IsDBNull(paramIdx))
            {
                return null;
            }
            else
            {
                return reader.GetDateTime(paramIdx);
            }
        }
    }
}
